<?php

//set required scripts
require("HTMLFunctions.php");
require("ValidationFunctions.php");
require("StockFunctions.php");
require('ac_db.inc.php');

//set error reporting
error_reporting(E_ERROR | E_WARNING);

//set credentials for database
setcredentials("tr2286","databases8620", "w4111b.cs.columbia.edu/ADB");

//create DB instance
$db = new Db("Proj1", "Part3");

//set email as permanent variable as it will be constantly used
$email;
$ticker;
$companyName;

//ensure that input is not blank which would mean that the site is illegally accessed
if(count($_POST) == 0){
	
	//if so, redirect to login
	echo "<meta http-equiv='refresh' content='0;url=http://localhost/login.php'>";
	exit;
}

//if the only input is email and stock i.e. the size is 2
else{
	
	//get ticker and email
	$email = $_POST['email'];
	$ticker = $_POST['ticker'];
	//$email = "tr2286@columbia.edu";
	//$ticker = "AAPL";
	$companyName = $db->getStockName($ticker);
	
	//call function to update the data
	$currentStockData = StockFunctions::getCurrentStockData($ticker);
	$db->updateStock($ticker, $currentStockData[0], $currentStockData[5], $currentStockData[3], $currentStockData[4], $currentStockData[2]);
	
	displayDetail($email, $ticker, $companyName, $currentStockData, $db);
}


//function to display the detail of the stock
function displayDetail($email, $ticker, $companyName, $currentStockData, $db){
	
	//call function to get the most current information
	//$detailedStockInfo = StockFunctions::getCurrentStockData($ticker);
	
	//create the html beginning
	HTMLFunctions::printHTMLHead("Detailed Information for $ticker");
	
	//create header bar
	HTMLFunctions::generateTitleBar("$ticker: $companyName", $email, $db);
		
	//create array for button
	$parameterArray = array();
	$parameterArray['email'] = $email;
	$parameterArray['ticker'] = $ticker;
	$parameterArray['price'] = $currentStockData[0];
	
	$sharesOwned = $db->getShareQuantity($email, $ticker);
	$db->updateStock($ticker, $currentStockData[0], $currentStockData[5], $currentStockData[3], $currentStockData[4], $currentStockData[2]);
	
	//create the actual html code for the content page
	//print heading code
	echo <<<END_HTML
	
	<!--now proceed to main body-->
	<div class="content">
		<div class="tdmaindiv">
		
END_HTML;

	//create individual table for buttons
	echo <<<END_HTML
	
	<table class="googletablebuttons1" cellpadding="0" cellspacing="0">
	<tr class="googletrbuttons1">
	<td>
END_HTML;
	
	//call funciton to create button to trade the stock
	HTMLFunctions::generateButton("tradeStock.php", "Trade This Stock", $parameterArray);
	
	//print table tags
	echo"</td><td>";
	
	//call funciton to create button to trade the stock
	HTMLFunctions::generateButton("addToInterestList.php", "Add To Interest List", $parameterArray);
	
	//print table tags
	echo"</td><td>";
	
	//call funciton to create button to trade the stock
	HTMLFunctions::generateButton("profile.php", "Return to Profile", $parameterArray);
	
	//print table tags
	echo"</td></tr></table>";

	//compute the image url
	$chartURL = "http://chart.finance.yahoo.com/w?s=$ticker";

	/************************** 
         README 
l1    =    Last    = $quote[0] 
c1    =    Change    = $quote[1] 
o    =    Opening    = $quote[2] 
h    =    High    = $quote[3] 
g    =    Low        = $quote[4] 
v    =    Volume    = $quote[5] 
***************************/ 
	echo <<<END_HTML
	<table>
		<tr>
			<td>
			<p>Current Price: {$currentStockData[0]}</p>
			</td><td>
			<p>Daily Volume: {$currentStockData[5]}
			</td>
		</tr>
		<tr>
			<td>
			<p>Daily High: {$currentStockData[3]}</p>
			</td><td>
			<p>Daily Low: {$currentStockData[4]}</p>
			</td>
		</tr>
		<tr>
			<td>
			<p>Open Price: {$currentStockData[2]}</p>
			</td>
			<td>
			</td>
		</tr>
	</table>
	
	<br>

	<table>
		<tr>
			<td>
			<p>The most current Yahoo Finance chart can be seen below</p>
			</td>
		</tr>
		<tr>
			<td>
			<img src="$chartURL" alt="Chart Could Not Be Displayed!"/>
			</td>
		</tr>
	</table>
	</div>
	
END_HTML;

	

	//close the connection with the db
	$db->closeSession();
	
	//call function to create the end
	HTMLFunctions::printHTMLEnd();
}



